package com.arpa.ntocc.common.controller;

import cn.hutool.core.bean.BeanUtil;
import com.arpa.ntocc.common.common.cache.PartyCache;
import com.arpa.ntocc.common.common.cache.PartyCacheDO;
import com.arpa.ntocc.common.domain.entity.Party;
import com.arpa.ntocc.common.domain.enums.ClientTypeEnum;
import com.arpa.ntocc.common.service.authorize.AuthorizeTokenService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 公共controller
 * @Author liuyj
 * @Date 2019-11-14 18:11:17
 */
public class BaseController {

    @Autowired
    public AuthorizeTokenService authorizeTokenService;

    @Autowired
    public PartyCache partyCache;

    /**
     * 获取当前用户code
     * @return
     */
    public String getCurrentPartyCode(){
        return authorizeTokenService.getCurrentPartyCode();
    }

    /**
     * 获取当前登录的客户端
     * @return
     */
    public ClientTypeEnum getCurrentClientType(){
        return authorizeTokenService.getCurrentClientType();
    }

    /**
     * 获取当前用户
     * @return
     */
    public Party getCurrentParty(){
        Party party=null;
        String partyCode=authorizeTokenService.getCurrentPartyCode();
        PartyCacheDO partyCacheDO=partyCache.get(partyCode);
        if(partyCacheDO!=null){
            BeanUtil.copyProperties(partyCacheDO,party);
        }
        return party;
    }

    /**
     * 获取当前用户账号（手机号）
     * @return
     */
    public String getCurrentPhone(){
        String partyCode=authorizeTokenService.getCurrentPartyCode();
        PartyCacheDO partyCacheDO=partyCache.get(partyCode);
        if(partyCacheDO!=null){
            return partyCacheDO.getPhone();
        }
        return null;
    }

    /**
     * 获取当前用户机构code
     * @return
     */
    public String getCurrentBranchCode(){
        String partyCode=authorizeTokenService.getCurrentPartyCode();
        PartyCacheDO partyCacheDO=partyCache.get(partyCode);
        if(partyCacheDO!=null){
            return partyCacheDO.getBranchCode();
        }
        return null;
    }
}
